package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Diagnose;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SurgeryMapper;
import com.ruoyi.system.domain.Surgery;
import com.ruoyi.system.service.ISurgeryService;

import javax.annotation.Resource;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 手术Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-21
 */
@Service
public class SurgeryServiceImpl implements ISurgeryService {
    @Resource
    private SurgeryMapper surgeryMapper;

    /**
     * 查询手术
     *
     * @param id 手术主键
     * @return 手术
     */
    @Override
    public Surgery selectSurgeryById(Long id) {
        return surgeryMapper.selectSurgeryById(id);
    }

    /**
     * 查询手术列表
     *
     * @param surgery 手术
     * @return 手术
     */
    @Override
    public List<Surgery> selectSurgeryList(Surgery surgery) {
        return surgeryMapper.selectSurgeryList(surgery);
    }

    /**
     * 新增手术
     *
     * @param surgery 手术
     * @return 结果
     */
    @Override
    public int insertSurgery(Surgery surgery) {
        surgery.setCreateTime(DateUtils.getNowDate());
        return surgeryMapper.insertSurgery(surgery);
    }

    /**
     * 修改手术
     *
     * @param surgery 手术
     * @return 结果
     */
    @Override
    public int updateSurgery(Surgery surgery) {
        surgery.setUpdateTime(DateUtils.getNowDate());
        return surgeryMapper.updateSurgery(surgery);
    }

    /**
     * 批量删除手术
     *
     * @param ids 需要删除的手术主键
     * @return 结果
     */
    @Override
    public int deleteSurgeryByIds(Long[] ids) {
        return surgeryMapper.deleteSurgeryByIds(ids);
    }

    /**
     * 删除手术信息
     *
     * @param id 手术主键
     * @return 结果
     */
    @Override
    public int deleteSurgeryById(Long id) {
        return surgeryMapper.deleteSurgeryById(id);
    }

    @Override
    public String importCase(List<Surgery> surgeryList, boolean updateSupport, String operName) {
        if (CollUtil.isEmpty(surgeryList)) {
            throw new ServiceException("导入数据不能为空！");
        }

        AtomicInteger index = new AtomicInteger(2);
        String username = getUsername();
        surgeryList.forEach(item -> {
            if (StrUtil.isBlank(item.getCode())) {
                throw new ServiceException("第" + index.get() + "行的手术编码不能为空！");
            }
            if (StrUtil.isBlank(item.getName())) {
                throw new ServiceException("第" + index.get() + "行的手术名称不能为空！");
            }
            Date nowDate = DateUtils.getNowDate();
            item.setCreateTime(nowDate);
            item.setCreateBy(username);
            item.setUpdateTime(nowDate);
            item.setUpdateBy(username);

            index.getAndIncrement();
        });
        //删除数据库数据
        surgeryMapper.delete();
        surgeryMapper.saveBatch(surgeryList);
        return "导入成功";
    }

    @Override
    public List<Surgery> listByName(List<String> names) {
        if (CollUtil.isEmpty(names)) {
            return CollUtil.newArrayList();
        }
        return surgeryMapper.listByName(names);
    }
}
